// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

var ClickManager = require('ClickManager')
var NetworkManager = require('NetworkManager')
var UnitTools = require('UnitTools')
var CreatorHelper = require('CreatorHelper')
var Config = require('Config')

cc.Class({
    extends: cc.Component,

    properties: {
        id: cc.Label,
        username: cc.Label,
        masonry: cc.Label,
        headimg: cc.Sprite,

        headimgBtn: cc.Node, // 头像按钮
        shopBtn: cc.Node,    // 充值按钮
        copyBtn: cc.Node,    // 复制按钮
        setBtn: cc.Node,     // 设置按钮
        helpBtn: cc.Node,    // 帮助按钮

        shopModel: cc.Prefab,  // 商品模态框
        helpModel: cc.Prefab,  // 帮助模态框
        setModel: cc.Prefab,   // 设置模态框
        headimgModel: cc.Prefab,    // 头像模态框
        alertModel: cc.Prefab, // 提示框
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        var scene = cc.director.getScene()

        // 头像事件
        ClickManager.onClick(this.headimgBtn, this, () => {
            this.showPrefabModel(this.headimgModel)
        })

        // 商城事件
        ClickManager.onClick(this.shopBtn, this, () => {
            this.showPrefabModel(this.shopModel)
        })

        // 复制事件
        ClickManager.onClick(this.copyBtn, this, () => {
            UnitTools.copyText(this.id)
            ClickManager.alertModel(this.alertModel, '复制成功')
        })

        // 设置事件
        ClickManager.onClick(this.setBtn, this, () => {
            this.showPrefabModel(this.setModel)
        })

        // 帮助事件
        ClickManager.onClick(this.helpBtn, this, () => {
            this.showPrefabModel(this.helpModel)
        })
    },

    // 显示prefab弹出层
    showPrefabModel (prefab) {
        var scene = cc.director.getScene()
        let prefabModel = cc.instantiate(prefab)
        prefabModel.parent = scene
        prefabModel.active = true
    },

    // 数据实时更新
    update (dt) {
        let playerBaseInfo = cc.sys.localStorage.getItem('__PLAYER_BASE_INFO__')
        if (!UnitTools.isNullOrUndefined(playerBaseInfo)) {
            playerBaseInfo = JSON.parse(playerBaseInfo)
            this.id.string = 'ID：' + playerBaseInfo.uid
            this.username.string = playerBaseInfo.username
            this.masonry.string = playerBaseInfo.balance
            CreatorHelper.changeSpriteFrame(this.headimg, Config.headPath + playerBaseInfo.headimg)
        }
    },
});
